/**
 * CEAport framework to create open APIs for communications enable applications.
 * https://ceaport.googlecode.com
 *
 * Copyright (c) 2008 by respective authors (see below).
 *
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Contributors:
 * Michael Weiss (michael.r.weiss@gmail.com)
 * 
 */
package org.ceaport.rest.server;

import java.util.*;

import org.ceaport.rest.server.events.EventConsumer;

public class Database implements EventConsumer {
	protected Conference conference;
	
	public Database() {
	}
	
	public Conference getConference(String room) {
		return conference;
	}

	public void setConference(String room, Conference conference) {
		this.conference = conference;
	}
	
	public List<Conference> getConferences() {
		List<Conference> conferences;
		conferences = new ArrayList<Conference>();
		conferences.add(conference);
		return conferences;
	}
	
	synchronized public void processEvent(String type, Map<String, String> attributes) {
		// String room = attributes.get("room");
		if (type.equals("join")) {
			String uid = attributes.get("user");
			String name = attributes.get("callerIdName");
			String phone = attributes.get("callerIdNumber");
			conference.addUser(uid, new User(name, phone));
		} else if (type.equals("leave")) {
			String uid = attributes.get("user");
			conference.removeUser(uid);
		} else if (type.equals("chat")) {
			String user = attributes.get("user");
			String message = attributes.get("message");
			conference.addMessage(user, message);
		}
	}
	
}
